import copy
from base import XMLBase, Reference, LinkType
from characterization import TargetObject, TargetProcess

class CollectionLink(LinkType):
    _node_name = 'ParentCollection'

class SimulationLink(LinkType):
    _node_name = 'Simulation'

class Collection(XMLBase):
    _node_name = 'Collection'
    _attrib_map = {'Name': 'name',
                   'Description': 'description'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference},
                 'TargetProcess':{'attribute':'target_processes',
                                  'class':TargetProcess},
                 'TargetObject':{'attribute':'target_objects',
                                 'class':TargetObject}}

    def __init__(self, name=None, description=None, parent_collection=None,
                 references=[], target_objects=[], 
                 target_processes=[]):

        self.name = name
        self.description = description
        self.parent_collection = parent_collection
        self.target_objects = copy.copy(target_objects)
        self.target_processes = copy.copy(target_processes)
        self.references = copy.copy(references)
    
    def _build_tuple(self):
        XMLBase._build_tuple(self)
        self._elements.insert(0, self.parent_collection.to_tuple())

    def read(self, xml_string):
        XMLBase.read(self, xml_string)
        parent_coll_nodes = self._xmldoc.getElementsByTagName('ParentCollection')

        if parent_coll_nodes:
            xmlstr = parent_coll_nodes[0].toxml()
            self.parent_collection = CollectionLink()
            self.parent_collection.read(xmlstr)


class Simulation(XMLBase):
    _node_name = 'Simulation'
    _attrib_map = {'Name': 'name',
                   'Description': 'description'}
    _list_map = {'Reference':{'attribute':'references',
                              'class':Reference},
                 'TargetProcess':{'attribute':'target_processes',
                                  'class':TargetProcess},
                 'TargetObject':{'attribute':'target_objects',
                                 'class':TargetObject}}

    def __init__(self, name=None, description=None, parent_collection=None,
                 references=[], target_objects=[], 
                 target_processes=[]):

        self.name = name
        self.description = description
        self.parent_collection = parent_collection
        self.target_objects = copy.copy(target_objects)
        self.target_processes = copy.copy(target_processes)
        self.references = copy.copy(references)

    def _build_tuple(self):
        XMLBase._build_tuple(self)
        self._elements.insert(0, self.parent_collection.to_tuple())

    def read(self, xml_string):
        XMLBase.read(self, xml_string)
        parent_coll_nodes = self._xmldoc.getElementsByTagName('ParentCollection')

        if parent_coll_nodes:
            xmlstr = parent_coll_nodes[0].toxml()
            self.parent_collection = CollectionLink()
            self.parent_collection.read(xmlstr)
